package com.uchiha.nova.core.tool.support;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uchiha.nova.core.tool.util.CollectionUtil;
import com.uchiha.nova.core.tool.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author cobot
 * @version 1.0.0
 * @date 2020/11/2
 */
@Slf4j
public class SqlKeyword {
    private final static String SQL_REGEX = "'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";

    /**
     * 条件构造器
     *
     * @param query 查询字段
     * @param qw    查询包装类
     */
    public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw) {
        if (CollectionUtil.isEmpty(query)) {
            return;
        }
        query.forEach((k, v) -> {

            if (StringUtil.isBlank(k) || v==null || StringUtil.isBlank(v.toString())) {
                return;
            }
            k= StringUtil.hump2underline(k);
            String value=v.toString();

            if (value.startsWith("gt(")) {
                qw.gt(k, value.substring(3, value.length() - 1));
            }else if (value.startsWith("gte(")) {
                qw.ge(k, value.substring(4, value.length() - 1));
            }else if (value.startsWith("lt(")) {
                qw.lt(k, value.substring(3, value.length() - 1));
            }else if (value.startsWith("lte(")) {
                qw.le(k, value.substring(4, value.length() - 1));
            }else if (value.startsWith("btw(")) {
                String[] strings = value.split(",");
                String s1 = strings[0].substring(4, strings[0].length());
                String s2 = strings[1].substring(0, strings[1].length() - 1);
                qw.between(k, s1,s2);
            } else if (value.startsWith("notbtw(")) {
                String[] strings = value.split(",");
                String s1 = strings[0].substring(7, strings[0].length());
                String s2 = strings[1].substring(0, strings[1].length() - 1);
                qw.notBetween(k, s1,s2);
            } else if (value.startsWith("null")) {
                qw.isNull(k);
            } else if (value.startsWith("notnull")) {
                qw.isNotNull(k);
            }else if (value.startsWith("in(")) {
                qw.in(k, (Object[]) value.substring(3, value.length() - 1).split(","));
            }else if (value.startsWith("notin(")) {
                qw.notIn(k, (Object[]) value.substring(6, value.length() - 1).split(","));
            }else if (value.startsWith("lk(")) {
                qw.like(k,value.substring(3, value.length() - 1));
            }else if (value.startsWith("notlk(")) {
                qw.notLike(k,value.substring(6, value.length() - 1));
            }else if (value.startsWith("leftlk(")) {
                qw.likeLeft(k,value.substring(7, value.length() - 1));
            }else if (value.startsWith("rightlk(")) {
                qw.likeRight(k,value.substring(8, value.length() - 1));
            }else if (value.startsWith("ne(")) {
                qw.ne(k,value.substring(3, value.length() - 1));
            }else {
                qw.eq(k, v);
            }
        });
    }
}
